package com.hlxt.nj.common;


import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PageBean implements Serializable
{
  private static final long serialVersionUID = 5997383423883313158L;
  private int currentPage;
  private int length;
  private int totalPages;
  private int totalRecords;
  private List results;
  private Map condition;

  public PageBean(int length)
  {
    this.length = length;
    this.results = new ArrayList();
    this.condition = new HashMap();
  }

  public PageBean(int currentPage, int length) {
    this.length = length;
    this.results = new ArrayList();
    this.condition = new HashMap();
    setCurrentPage(currentPage);
    setLength(length);
  }
  
  public List getResults() {
    return this.results;
  }

  public void setResults(List results) {
    this.results = results;
  }

  public int getCurrentPage() {
    return this.currentPage;
  }

  public void setCurrentPage(int currentPage) {
    this.currentPage = currentPage;
  }

  public int getLength() {
    return this.length;
  }

  public void setLength(int length) {
    if (length < 1) {
      throw new IllegalArgumentException("length must greater than 0 !");
    }
    this.length = length;
  }

  public int getTotalPages()
  {
    return this.totalPages;
  }

  public int getTotalRecords() {
    return this.totalRecords;
  }

  public void setTotalRecords(int count) {
    this.totalRecords = count;
    this.totalPages = (this.totalRecords / this.length);
    if (this.totalRecords % this.length != 0)
      this.totalPages += 1;
    if (this.totalPages != 0) {
      if (this.currentPage < 1)
        this.currentPage = 1;
      if (this.currentPage > this.totalPages)
        this.currentPage = this.totalPages;
    } else {
      this.currentPage = 1;
    }
  }

  public boolean canToFirst() {
    return this.currentPage > 1;
  }

  public boolean canToLast() {
    return this.currentPage < this.totalPages;
  }

  public boolean canToNext() {
    return this.currentPage < this.totalPages;
  }

  public boolean canToPre() {
    return this.currentPage > 1;
  }

  public int getRsFirstNumber() {
    return (this.currentPage - 1) * this.length + 1;
  }

  public int getRsLastNumber() {
    return this.currentPage * this.length + 1;
  }

  public String toString() {
    StringBuilder sb = new StringBuilder();
    return 
      new StringBuilder("totalRecords=").append(
      this.totalRecords).toString() + 
      new StringBuilder(";totalPages=").append(this.totalPages)
      .toString() + 
      new StringBuilder(";currentPage=")
      .append(this.currentPage).toString() + 
      new StringBuilder(";length=").append(this.length)
      .toString();
  }

  public Map getCondition() {
    return this.condition;
  }

  public void setCondition(Map condition) {
    this.condition = condition;
  }
}
